<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <link rel="icon" href="./Logo.jpg" type="image/x-icon">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户中心 - 寻香</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="common.css">
    <style>
        :root {
            --primary-color: #2d5a27;
            --secondary-color: #8ba888;
            --accent-color: #e8b059;
            --text-color: #333;
            --light-bg: #F8F1E4;
        }

        body {
            font-family: 'Arial', '微软雅黑', sans-serif;
            background: var(--light-bg);
            margin: 0;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding-top: 80px;
        }

        .auth-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            width: 90%;
            max-width: 400px;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .auth-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, var(--primary-color), var(--accent-color));
        }

        .tabs {
            display: flex;
            margin-bottom: 20px;
            border-bottom: 2px solid #eee;
        }

        .tab {
            flex: 1;
            text-align: center;
            padding: 15px;
            cursor: pointer;
            color: #666;
            transition: all 0.3s;
        }

        .tab.active {
            color: var(--primary-color);
            border-bottom: 2px solid var(--primary-color);
            margin-bottom: -2px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
        }

        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.3s;
        }

        input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(45, 90, 39, 0.1);
            outline: none;
        }

        .auth-button {
            width: 100%;
            padding: 12px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .auth-button:hover {
            background: #1e4a1e;
        }

        .form-footer {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }

        .form-footer a {
            color: var(--primary-color);
            text-decoration: none;
        }

        /* 错误提示样式 */
        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        /* 成功提示样式 */
        .success-message {
            color: #27ae60;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="tabs">
            <div class="tab active" data-tab="login">登录</div>
            <div class="tab" data-tab="register">注册</div>
        </div>

        <!-- 登录表单 -->
        <form id="loginForm" class="auth-form">
            <div class="form-group">
                <label for="loginEmail">邮箱</label>
                <input type="email" id="loginEmail" required>
                <div class="error-message" id="loginEmailError"></div>
            </div>
            <div class="form-group">
                <label for="loginPassword">密码</label>
                <input type="password" id="loginPassword" required>
                <div class="error-message" id="loginPasswordError"></div>
            </div>
            <button type="submit" class="auth-button">登录</button>
            <div class="form-footer">
                <a href="#" id="forgotPassword">忘记密码？</a>
            </div>
        </form>

        <!-- 注册表单 -->
        <form id="registerForm" class="auth-form" style="display: none;">
            <div class="form-group">
                <label for="registerEmail">邮箱</label>
                <input type="email" id="registerEmail" required>
                <div class="error-message" id="registerEmailError"></div>
            </div>
            <div class="form-group">
                <label for="registerUsername">用户名</label>
                <input type="text" id="registerUsername" required>
                <div class="error-message" id="registerUsernameError"></div>
            </div>
            <div class="form-group">
                <label for="registerPassword">密码</label>
                <input type="password" id="registerPassword" required>
                <div class="error-message" id="registerPasswordError"></div>
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" required>
                <div class="error-message" id="confirmPasswordError"></div>
            </div>
            <button type="submit" class="auth-button">注册</button>
        </form>
    </div>

    <script src="common.js"></script>
    <script>
        // 切换登录/注册表单
        const tabs = document.querySelectorAll('.tab');
        const forms = document.querySelectorAll('.auth-form');

        tabs.forEach(tab => {
            tab.addEventListener('click', () => {
                const formType = tab.dataset.tab;
                
                // 切换标签样式
                tabs.forEach(t => t.classList.remove('active'));
                tab.classList.add('active');

                // 切换表单显示
                forms.forEach(form => {
                    form.style.display = form.id === `${formType}Form` ? 'block' : 'none';
                });
            });
        });

        // 表单验证和提交
        const loginForm = document.getElementById('loginForm');
        const registerForm = document.getElementById('registerForm');

        loginForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            const email = document.getElementById('loginEmail').value;
            const password = document.getElementById('loginPassword').value;

            try {
                // 这里添加登录逻辑
                const response = await login(email, password);
                if (response.success) {
                    // 登录成功，保存用户信息到localStorage
                    handleLoginSuccess(response.user);
                }
            } catch (error) {
                document.getElementById('loginEmailError').textContent = error.message;
                document.getElementById('loginEmailError').style.display = 'block';
            }
        });

        registerForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            const email = document.getElementById('registerEmail').value;
            const username = document.getElementById('registerUsername').value;
            const password = document.getElementById('registerPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            // 密码验证
            if (password !== confirmPassword) {
                document.getElementById('confirmPasswordError').textContent = '两次输入的密码不一致';
                document.getElementById('confirmPasswordError').style.display = 'block';
                return;
            }

            try {
                // 这里添加注册逻辑
                const response = await register(email, username, password);
                if (response.success) {
                    // 注册成功，自动登录
                    handleLoginSuccess(response.user);
                }
            } catch (error) {
                document.getElementById('registerEmailError').textContent = error.message;
                document.getElementById('registerEmailError').style.display = 'block';
            }
        });

        // 模拟API调用
        async function login(email, password) {
            // 从localStorage获取用户数据
            const users = JSON.parse(localStorage.getItem('users')) || {};
            
            if (users[email] && users[email].password === password) {
                return {
                    success: true,
                    user: {
                        email: email,
                        username: users[email].username,
                        teaTree: users[email].teaTree || {
                            level: 1,
                            experience: 0,
                            type: '龙井',
                            lastLogin: null
                        }
                    }
                };
            } else {
                throw new Error('邮箱或密码错误');
            }
        }

        async function register(email, username, password) {
            // 从localStorage获取用户数据
            const users = JSON.parse(localStorage.getItem('users')) || {};
            
            // 检查邮箱是否已注册
            if (users[email]) {
                throw new Error('该邮箱已被注册');
            }

            // 保存新用户数据
            users[email] = {
                username,
                password,
                teaTree: {
                    level: 1,
                    experience: 0,
                    type: '龙井',
                    lastLogin: null
                }
            };
            
            localStorage.setItem('users', JSON.stringify(users));
            
            return {
                success: true,
                user: {
                    email,
                    username,
                    teaTree: users[email].teaTree
                }
            };
        }

        // 用户登录成功后的处理
        function handleLoginSuccess(userData) {
            // 保存当前登录用户数据
            localStorage.setItem('currentUser', JSON.stringify(userData));
            
            // 跳转到首页
            window.location.href = 'menu.html';
        }
    </script>
</body>
</html> 